<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\common\model\Jishu as JishuModel;
use app\common\model\Jishucate as JishucateModel;

/*技术*/

class Jishu extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $keyword = input('keyword') ?: '';
        $datas = JishuModel::where('title', 'like', "%{$keyword}%")->order('id', 'desc')->paginate(10);
        return view('', compact('datas', 'keyword'));
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //分类数据
        $jishuCates = JishucateModel::all();
        return view('', compact('jishuCates'));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->post();
        unset($data['file']);
        $data['time']=time();
        $Product = new JishuModel();
        $Product->data($data);
        $Product->save();
        $this->success('添加成功', '/admin/jishu');

    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //分类数据
        $jishuCates = JishucateModel::all();
        //技术数据
        $data = JishuModel::get($id);
        return view('', compact('jishuCates', 'data'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //表单数据
        $data = $request->post();
        unset($data['_method']);
        unset($data['file']);
        JishuModel::where('id', $id)->update($data);
        $this->success('编辑成功', '/admin/jishu');
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //删除数据
        if (JishuModel::where('id', $id)->delete()) {
            $res['status'] = '1';
            $res['msg'] = '删除成功';
        } else {
            $res['status'] = '0';
            $res['msg'] = '删除失败';
        }
        return $res;
    }
}
